home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / txtvid.exe / TXTVIDEO.C < prev    next >
Text File  |  1992-10-15  |  11KB  |  461 lines

  1. /* Author:  Everett Anderson
  2.         CIS: 74630,632
  3.         WWIVlink: 1@15101
  4.  
  5.         Through 1994:  BBS at (501) 663-2789 */
  6.  
  7. /* Written and compiled with TC++ v1.01 */
  8.  
  9. /* Improvements welcome! */
  10.  
  11. /* High level, home made video functions for quick select bar programs */
  12.  
  13. /* I wrote these in the process of exploring C's color text abilities.  You
  14.    can tell that I'm a fairly inexperienced programmer in some of the
  15.    functions.  Most of them follow more of a human math process than what
  16.    the language is probably capable of.  I decided not to make a real
  17.    window structure, as the line[][] array seemed so much more simple.
  18.    Hopefully, I'll come back to these lowly routines and write
  19.    them in C++. */
  20.  
  21. /* They have their limits.  I made the whole selection system based on a
  22.    character, not an integer.  This way, it works with both quick switch()
  23.    statements, and also lets the user enter a character rather than using
  24.    the select bar.  Currently, no Ctrl or Alt characters are supported. */
  25.  
  26. /* The whole system is based on a "feed string".  I think this is a fairly
  27.    basic way to handle it (dare I say original or intelligent).  All you
  28.    have to give my putbox function is the feed string, the top and left
  29.    position on a 1,1,80,25 screen that you wish it to be, and an optional
  30.    header.  The width, length, and the selection process are all
  31.    automatically factored in, without you worrying about it.  */
  32.  
  33. /* Here's an example feed string:
  34.  
  35.    sprintf(s,"[1].  Eat\n[2].  Drink\n[3].  Sleep");
  36.  
  37.    The window will be made, a selection bar will be used, and the possible
  38.    options are 1 2 3.  The user can use a select bar or just enter the
  39.    highlighted option.  Okay, so maybe that's more than you wanted to give
  40.    this lowly function system, but you've downloaded it, so improve on it!
  41.    You can also incorporate the selection anywhere in the phrase, such as
  42.  
  43.    sprintf(s,"[E]at\nDr[i]nk\nSl[e]ep");
  44.    */
  45.  
  46. /* If you use these, just cut out the main function and link the obj.  I'm just
  47.    combining them so that they don't get separated, and so that I don't have
  48.    to write a makefile to upload this. */
  49.  
  50. /* Final note:  There is no system (as there could be in C++) of easily going
  51.    back to the menu that you may have had five windows ago.  Putbox() just
  52.    makes and controls one window at a time.  I commented out the three
  53.    lines that would let a user abort with an ESC and return an ESC value.  */
  54.  
  55. #include <stdio.h>
  56. #include <conio.h>
  57. #include <string.h>
  58.  
  59. int NORMAL = 15;             /* Default colors */
  60. int SELECT = 14;
  61. int BORDER = 11;
  62. int BACKGROUND = 1;
  63. int BAR = 3;
  64.  
  65. #define UP    72
  66. #define DOWN  80
  67. //#define ESC   27
  68.  
  69. int lines, winright;
  70. char line[26][85], options[36];      /* Main concept of line[][] and options[] */
  71.  
  72. /* Prototypes */
  73. void cls(void);
  74. void strchar(char *string, const char ch);
  75. void box(int left, int top, int right, int bottom);
  76. int get_box_width(char *s);
  77. void putline(int y, char *s);
  78. void putbar(int y, int textline);
  79. void delbar(int y, int textline);
  80. char getbar(void);
  81. void putheader(int width, char *header);
  82. void format_lines(char *s);
  83. void format_options(void);
  84. char selection(void);
  85. void new_window(void);
  86. char putbox(char *string, int left, int top, char *header);
  87.  
  88. void cls(void)
  89. /* Clears the screen, setting a text window */
  90. {
  91.  textcolor(LIGHTGRAY);
  92.  textbackground(BLACK);
  93.  window(1,1,80,25);
  94.  clrscr();
  95. }
  96.  
  97. void strchar(char *string, const char ch)
  98. /* This acts in the same way as strcat except it combines a string and
  99.    a single character, updating the null at the end. */
  100. {
  101.   int last;
  102.  
  103.   last=strlen(string);
  104.   string[last]=ch;
  105.   string[last+1]=0;
  106. }
  107.  
  108. void box(int left, int top, int right, int bottom)
  109. /* Only works with lines 1-24.  Line 25 scrolls */
  110. /* This makes a single-line border box */
  111. {
  112.   int x, y;
  113.  
  114.   window(1, 1, 80, 25);
  115.  
  116.   window(left, top, right, bottom+1);
  117.  
  118.   winright=right-left;
  119.  
  120.   x=1;
  121.   y=1;
  122.  
  123.   gotoxy(1, 1);
  124.   cputs("┌");
  125.   while (x++<right-left)
  126.     cputs("─");
  127.   cputs("┐");
  128.   while (y++<bottom-top-1) {
  129.       gotoxy(1, y);
  130.       cputs("│");
  131.       clreol();
  132.       gotoxy(right-left+1, y);
  133.       cputs("│");
  134.       }
  135.   gotoxy(1, bottom-1);
  136.   cputs("└");
  137.   x=1;
  138.   while (x++<right-left)
  139.      cputs("─");
  140.   cputs("┘");
  141.   window(left, top, right, bottom);
  142. }
  143.  
  144. int get_box_width(char *s)
  145. /* Calculates the necessary box width when given a feed string to work
  146.    from */
  147. {
  148.   int pos, length, largest;
  149.  
  150.   pos=0;
  151.   length=0;
  152.   largest=0;
  153.  
  154.   while (pos<strlen(s)) {
  155.       if (s[pos]=='[') {
  156.       pos+=3;
  157.       length+=1;
  158.       continue;
  159.       }
  160.       if ((s[pos]=='\n') || (s[pos]=='\r')) {
  161.      if (length>largest) {
  162.          largest=length;
  163.          length=0;
  164.          pos++;
  165.          continue;
  166.          }
  167.      length=0;
  168.       } else
  169.      length++;
  170.       pos++;
  171.       }
  172.   return(largest);
  173. }
  174.  
  175. void putline(int y, char *s)
  176. /* Puts text s on line y in the current text window, using correct color */
  177. {
  178.   int pos, done, option;
  179.  
  180.   gotoxy(3, y);
  181.   cputs("  ");
  182.   pos=0;
  183.   done=0;
  184.   option=0;
  185.  
  186.   while (!done) {
  187.       if (s[pos]=='[') {
  188.        textcolor(SELECT);
  189.        pos++;
  190.        cprintf("%c",s[pos]);
  191.        pos+=2;
  192.        textcolor(NORMAL);
  193.        option=1;
  194.        }
  195.       if (s[pos]!='\n')
  196.     cprintf("%c",s[pos]);
  197.       pos++;
  198.       if (pos==strlen(s))
  199.      done=1;
  200.       }
  201.   if (option)
  202.     for (done=pos; done<winright-3; done++)
  203.       cputs(" ");
  204. }
  205.  
  206. void putbar(int y, int textline)
  207. /* Positions the select bar at line y over the string line[textline] */
  208. {
  209.   textbackground(BAR);
  210.   putline(y, line[textline]);
  211. }
  212.  
  213. void delbar(int y, int textline)
  214. /* Removes the select bar from line y, rewriting line[textline] */
  215. {
  216.   textbackground(BACKGROUND);
  217.   putline(y, line[textline]);
  218. }
  219.  
  220. char getbar(void)
  221. /* Performs all bar movement operations until the selection is made w/
  222.    the enter key or with a possible selection.  Returns the correct
  223.    character selection. */
  224. {
  225.   int y, textline, ch, mods, i;
  226.   char check, *ptr;
  227.  
  228.   y=3;
  229.   textline=0;
  230.   ch=0;
  231.   while (!strchr(line[textline],'[')) {
  232.      y++;
  233.      textline++;
  234.      if (y>lines+2) {
  235.     y=3;
  236.     textline=0;
  237.     break;
  238.     }
  239.      }
  240.  
  241.   while (ch==0) {
  242.     putbar(y, textline);
  243.  
  244.     ch=getch();
  245.  
  246.     if ((ch==UP) || (ch==DOWN)) {
  247.      if (ch==UP) {
  248.          delbar(y, textline);
  249.          y--;
  250.          textline--;
  251.          while (!strchr(line[textline],'[')) {
  252.         y--;
  253.         textline--;
  254.         if (y<3) {
  255.           y=lines+2;
  256.           textline=lines-1;
  257.           }
  258.         }
  259.          }
  260.      if (ch==DOWN) {
  261.          delbar(y, textline);
  262.          y++;
  263.          textline++;
  264.          while (!strchr(line[textline],'[')) {
  265.         y++;
  266.         textline++;
  267.         if (y>lines+2) {
  268.           y=3;
  269.           textline=0;
  270.           }
  271.         }
  272.          }
  273.      if (y<3) {
  274.          y=lines+2;
  275.          textline=lines-1;
  276.          }
  277.      if (y>lines+2) {
  278.          y=3;
  279.          textline=0;
  280.          }
  281.      ch=0;
  282.      continue;
  283.      }
  284.     if ((ch=='\n') || (ch=='\r')) {
  285.     ptr=strchr(line[textline],'[');
  286.     check=line[textline][ptr-line[textline]+1];
  287.     ch=check;
  288.     continue;
  289.     }
  290. //    if (ch==ESC)
  291. //      continue;
  292.     if ((ch > '`') && (ch < '{'))
  293.       ch = ch - 32;
  294.     if (!strchr(options,ch))
  295.       ch=0;
  296.     }
  297.  
  298.   delbar(y, textline);
  299.   return(ch);
  300. }
  301.  
  302. void putheader(int width, char *header)
  303. /* Attempts to put a centered text header on the current box */
  304. {
  305.   int start, size;
  306.  
  307.   if (header[0]==0)
  308.     return;
  309.   size=strlen(header)+2;
  310.   if (size>width-4)
  311.      return;
  312.   start=(width/2)-(size/2);  /* Adding 1 usually corrects some header
  313.                 misalignments */
  314.   gotoxy(start, 1);
  315.   textcolor(SELECT);
  316.   cprintf(" %s ",header);
  317.   textcolor(NORMAL);
  318. }
  319.  
  320. void format_lines(char *s)
  321. /* Processes the feed string into line[][] format */
  322. {
  323.   int pos;
  324.  
  325.   lines=0;
  326.  
  327.   for(pos=0; pos<=strlen(s); pos++)
  328.       if ((s[pos]!='\n') && (s[pos]!='\r'))
  329.     strchar(line[lines],s[pos]);
  330.       else
  331.     lines++;
  332.   lines++;
  333.   for (pos=lines; pos<=25; pos++)
  334.      line[pos][0]=0;
  335. }
  336.  
  337. void format_options()
  338. /* Processes the feed string to get the specified selection options */
  339. {
  340.   int pos, i;
  341.   char *ptr;
  342.  
  343.   for(pos=0; pos<sizeof(options);